Hi,

M. Lewis <[EMAIL PROTECTED]> asked:
> You've lost me here Thomas. Again, his code was:
> 
> open my $fh, '<', $input_file or die qq(Cannot open 
> "$input_file": $!);
> 
> What am I missing here?

Charles wants to output an error message like

Cannot open "somefilename": No such file or directory at somescript line xx.

The part behind the colon is the error message in $!.

The naive way to construct this print would be to write

print 'Cannot open"' . $input_file . '":' . $!;

This is horrible, since Perl can interpolate variable names
in strings quoted using double quotes.
However, if you want to use the double quote character 
inside of such a string, you have to escape that character
in order to distinguish it from a string ending quote:

print "Cannot open \"$input_file\": $!";

This is where the qq() operator comes in. It basically
acts like a double quote for its contents, without actually
using that character - so you can now have a string that
can be interpolated but in which the doubles quotes do not
have to be escaped:

print qq(Look Ma! No "backslash"!);

And if you wanted to use round brackets inside your string,
you could use a different set of delimiters, like so:

print qq/Again, no escape character required for "()"/;

See the perlop manpage for details, and qq's useful
relatives q, qr and qw.

HTH,
Thomas

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to