Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight


by Raymond (Novice)
on Aug 05, 2013 at 20:47 UTC ( #1047987=perlquestion: print w/replies, xml ) Need Help??
Raymond has asked for the wisdom of the Perl Monks concerning the following question:

#!/usr/bin/perl use strict; use warnings; #extract column with unpack $a = "To be or not to be"; $b = unpack("x6 A6", $a); #skip 6, grab 6 print $b; ($b, $c) = unpack("x6 A2 x5 A2", $a); #forward 6, grab 2; backward 5 +, grab 2 print "$b\n$c\n";

Global symbol "$c" requires explicit package name at line 10.

Global symbol "$c" requires explicit package name at line 11.

Execution of aborted due to compilation errors.
Ok i will use diagnostics, thank you; this examples are from a book, there is more than one, and don't know if the book has a resolution or if it's mistaken...

Replies are listed 'Best First'.
Re: example
by choroba (Chancellor) on Aug 05, 2013 at 20:52 UTC
    Your post does not contain any question. You are using strict but not declaring your variables, which is the reason of the errors. $a and $b do not have to be declared because they are special - see perlvar.
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

      $a and $b do not have to be declared because they are special

      Which means that they should probably avoided for purposes other than what they are designed and earmarked for (basically sorting operations).

      @Raymond: if you *really* need to use single letter variables, start off with $c, avoid $a and $b. But don't you think it would be better to give meaningful names to your variables? I hold the opinion that giving meaningful names to variables, subroutines, filehandles, etc., is the #1 way of correctly commenting my code. Therefore, asides from quick tests under the debugger, I almost never use one-letter variable, with just one exception: loop variables, i.e. when the variable does not have any other meaning than looping through the subscripts of an array, where I find variable names such as $i, $j and $k to be acceptable. But, whereas using such loop variable is quite common in a language like C (where I come from), it is much less often useful in Perl, in which we can most of the time use better looping constructs (which usually loop directly over the elements, rather than the subscripts, of an array, thereby making meaningful names (the content of the array elements) again possible.

Re: example
by toolic (Bishop) on Aug 05, 2013 at 20:59 UTC
Re: example
by AnomalousMonk (Canon) on Aug 06, 2013 at 13:18 UTC
    ($b, $c) = unpack("x6 A2 x5 A2", $a);   #forward 6, grab 2; backward 5, grab 2

    And just to avoid another blind alley, the unpack template specifier for moving backward in a string is  'X' (upper case), not  'x' (lower case). See pack for a complete specification of pack/unpack template specifiers; also see the pack tutorial perlpacktut.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1047987]
Approved by toolic
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2017-03-29 00:59 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (343 votes). Check out past polls.