2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2020

03/03/2003: Reading a whole file into a scalar Using Perl

Reading a whole file into a scalar Using Perl

Quite often, it is very useful to read the contents of a file into a scalar variable. The following example shows how this can be done. $fileName is the name of the input file. $buffer is a scalar where the file contents is stored. The $/ variable contains the end-of-record delimitor. By undefining the variable, Perl will be forced to read the entire file in one shot.

Usage:

$buffer = readFileContents($fileName)

Code:

sub readFileContents {
  my($fileName) = shift;
  my($buffer);
  local($/) = undef;

  open(FILE, $fileName)
    or die "couldn't open $filename for reading: $!\n";

  $buffer = <FILE>;
  close(FILE);
  return($buffer);
}

03/03/2003: How Do I Store Text in the Windows Clipboard Using JavaScript?

How Do I Store Text in the Windows Clipboard Using JavaScript?

Click on the first line of text in order to see that text pasted into the third line of text:

<HEAD>
<SCRIPT>
function IsWhitespace(ch) {
  return(
    ch == ' ' || ch == '\n' || ch == '\r' ||
    ch == '\t' || ch == '\f' || ch == '\v' || ch == '\b'
  );
}

// SaveToClibboard
function S2CB() {
  maxLen = 80;
  numLines = event.srcElement.innerText.length / maxLen;
  newStr = '';
  startPos = 0;
  for (x = 0; x < numLines; x++) {
    endPos = startPos + maxLen;
    while (! IsWhitespace(event.srcElement.innerText.charAt(endPos)))
      endPos--;
    newStr = newStr + event.srcElement.innerText.substring(startPos, endPos) + '\n';
    startPos = endPos + 1;
  }
  newStr = newStr + event.srcElement.innerText.substring(
    startPos, event.srcElement.innerText.length
  ) + '\n';
  window.clipboardData.setData('Text', newStr);
  oSource2.innerText = window.clipboardData.getData('Text');
}
</SCRIPT>
</HEAD>
<BODY>
<B ID="oSource" onClick="S2CB()">@changes 2000-Nov-01
  DMM DMM00013 Silentsurf(nph-index.cgi,Interface.pm):
  Initial hits to the server are immediately routed to the startup
  form which eliminates a lot of module loading in the
  Silentsurf package./</B><br>
<B ID="oSource1" onClick="S2CB()">222</B><br>
<pre>!!<B ID="oSource2" onClick="S2CB()">333</B>!!</pre><br>
</BODY>
</HTML>