in reply to variable expansion
I have the reading in part figured out, :) but how do I substitute the variables for their values (which will change as I run the program)?
I confess that I'm uncertain of the exact question you are asking, so I'll try to answer the easiest question I can make of your post. :-)
Are you asking:
How can I print out the values of scalar variables?
If so, you're in luck. Perl makes this much easier than other C derived languages. The one way to print out values it like this:
print "I like " . $x . "\n";
This code uses the dot concatentation operator to join strings together. There is a little magic going on here, in that the value of $x is treated like a string. Consider this code:
$x = '0001'; print "I like " . $x; print "but I like " . ($x + 0)
The first print statement will yield "I like 0001", but the second prints "but I like 1". The expression with the + operator in the second print statement yeilds a number, rather than a string. Therefore, the leading zeros are removed before the concatentation occurs ($x remains '0001').
Perl provides an even simpler way to print out scalars -- interpolation. Perl will attempt to substitute the value for scalars and arrays in double quoted strings.
$x = '0001'; print "I like $x";
This code prints "I like 0001". Remember that single quoted strings do not interpolate variables.
$x = '0001'; print 'I like $x';
This prints "I like $x". You can prevent variable interpolation in double quoted strings by escaping the sigil (the punctuation mark that precedes variable names).
$x = 'timmah!'; print "\$x is $x";
This prints "$x is timmah!".
For more tips on string interpolation, checkout the perlop manpage under the section "Gory details of parsing quoted constructs".
|
|---|