Debugging Practice

Bill Jones bill at fccj.org
Mon Jun 14 12:07:40 CDT 1999


On the Jax.PM jacksonville-pm-list;
"Bill Jones" <bill at fccj.org> wrote -


> On the Jax.PM jacksonville-pm-list;
> "Bill Jones" <bill at fccj.org> wrote -
>
>
> #Reducing the problem to smallest test code:
> $vFlag = "a|0|0|y|0|0|x|x|z";
>
> print
>       " Well? ",
> {
>     ((split(/\|/, $vFlag))[7] eq 'x') ? 'do this' : 'do that'
> },
>       "\n";
>
>
> Please post your answers, etc, to -
>
>     jacksonville-pm-list at hfb.pm.org
>
> Also, please elaborate upon which paths and resources
> you followed to determine the correct answer...


OK, sorry it took me so long to get back with
the answer (IE, the answer I have.)

If you do a perl -c code.pl it will say the
syntax is correct...

However, If you run the code, it gives an error like -

# Odd number of elements in hash list.
File 'Untitled'; Line 1
 Well? HASH(0x97dad28)


Many people responded to this request. And yes,
there is more than on way to do it -


(All of the following will work.)

# Way 1 (eval: dangerous if used with user supplied data):
print " Well? ",
eval { ((split(/\|/, $vFlag))[7] eq 'x') ? 'do this' : 'do that' }, "\n";

# Way 2 (do):
$vFlag = "a|0|0|y|0|0|x|x|z";
print " Well? ",
do { ((split(/\|/, $vFlag))[7] eq 'x') ? 'do this' : 'do that' }, "\n";

# Way 3 (remove the damn Squiggly Brackets :)
$vFlag = "a|0|0|y|0|0|x|x|z";
print " Well? ",
    ((split(/\|/, $vFlag))[7] eq 'x') ? 'do this' : 'do that', "\n";

# Of Note:  JProctor recognized I had ask you to
# DEBUG as opposed to IMPROVE the code:
# His responses were -
$out = ((split('|', $vFlag))[7] eq 'x' ? 'yes' : 'no';
print "Well? $out \n";

or...

@flags = split('|', $vFlag);
for ($i = 0; $i < $#flags; $i++)
    {
    $out[$i] = $flags[$i] eq 'x' ? 'yes' : 'no';
    }
print "Well? $out[7] \n";

# He finally settled upon removing the {} (saying they
# were vaguely hash-like. :)


# Way 4 (using @{[]}; Interpolation)
# The answer I settled upon (before even posting this query
# to the group) was -

$vFlag = "a|0|0|y|0|0|x|x|z";
print " Well? ",
@{[
    ((split(/\|/, $vFlag))[7] eq 'x') ? 'do this' : 'do that'
]}, "\n";

Interpolation by @{[]}; is powerful in that you gain the ability in
'eval'uation of complex code with out running the risks that using 'eval' by
it's self would introduce to the script.

The best way in this case would have to been to simply remove the HASH-like
reference unintentionally introduced by using {} ...

Where is @{[]} used in the real world?  Well, inside 'HERE' Docs you cannot
use a system call with either `` (backticks) or systems(), you have to
basically do something like -

print << __HERE_Doc;
Your Unix ENVIRONMENT:
@{[ die if ((system("set")) > 0) ]};

(For an example, see http://jacksonville.pm.org/source/sysadmin)
__HERE_Doc


Reference: Look up Perl FAQ 4 - Data Manipulation.


I hope this has been beneficial to all :)
I hope you all Enjoy Perl More!


seek DATA,0,1;print<DATA>;
__DATA__   -Sneex-  :]
Just Another Perl Monger...
_________________________________________________________________________
Bill Jones  | Data Security Specialist | http://www.fccj.org/cgi/mail?dss
FCCJ  |  501 W State St  |  Jacksonville, FL 32202  |  1 (904) 632-3089

$_="JOIN THE JAX PERL MONGERS J zbjrnmuhkkd Pdqk Mnmfdqr J\n";
tr/za-y/a-z/; / ((.)) /; do { print substr($_,25); }
while ( s/((.).{23}) $2 (.)(.*) (.)$/$1$5 $2 $4 $3/ )


The Jacksonville Perl Monger's Group is operated by -
Bill -Sneex- Jones ( sneex at usa.net ),
to whom send all praises, complaints, or comments...




More information about the Jacksonville-pm mailing list