Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

In my script, I have declared the variable with my as below.
my( $my_tgid_list, $my_switchClli_list, $my_btfn_list, $my_custClli_li +st, $my_trkCount_list, $ $my_ldn_list, $my_location_list, $my_status_ +list, $my_flag_list, $eachrow, $tmp_tgid, $tmp_switchClli, $tmp_btfn, + $tmp_custClli, $tmp_trkCount, $tmp_ldn, $tmp_location, $tmp_flag );
I get a compilation error saying
Can't declare scalar dereference in my at /trunkus/ui/bin/modTrunkGrpSave.pl line 146, near ");" /trunkus/ui/bin/modTrunkGrpSave.pl had compilation errors.
Please help.
Thanks in advance

2006-04-28 Retitled by planetscape, as per Monastery guidelines
Original title: 'Perl compilation error'

Replies are listed 'Best First'.
Re: Scalar dereference error
by NetWallah (Canon) on Apr 28, 2006 at 04:39 UTC
    Did you notice the extra "$" in your declaration:
    ..., $ $my_ldn_list, ...

         "For every complex problem, there is a simple answer ... and it is wrong." --H.L. Mencken

      Thanks a lot. I removed the extra $ & its working fine now.
Re: Scalar dereference error
by graff (Chancellor) on Apr 28, 2006 at 05:41 UTC
    It'll be easier to avoid this sort of bothersome (and embarrassing) glitch if you try one or more of the following strategies when writing your code:

    • Put some line breaks with proper indentation into that list -- e.g.:
      my ( $my_tgid_list, $my_switchClli_list, $my_btfn_list, $my_custClli_list, ... );
    • Don't declare all those lexical variables in one place -- if a bunch of them are only being used within relatively small blocks of code, their names should really only occur within those blocks, not at the top of a long script.

    • Consider grouping some of those variables into hashes -- e.g.:
      my @common_keys = qw/custClli switchClli trkCount tgid btfn flag locat +ion ldn .../; my ( %tmp, %mylist ); # use @common_keys with each hash

    Regarding the second point, maybe that long declaration line you posted is already properly "localized", such that all those variables really are required throughout the scope of the code block that contains that line. But if that code block is a whole big long script, you might need to think more carefully about design, and how to make your code more modular. At the very least, it may still be helpful to move some declarations closer to where they actually get used.

    OTOH, if you are just declaring all your variables with "my" at the top of your script regardless of where they actually get used, then you are missing out on some of the important advantages of "use strict" and lexically scoped "my" variables.

Re: Scalar dereference error
by Zaxo (Archbishop) on Apr 28, 2006 at 04:41 UTC

    You have a surplus '$' before $my_ldn_list. That's trying to dereference $my_ldn_list before it exists.

    After Compline,
    Zaxo

Re: Scalar dereference error
by hesco (Deacon) on Apr 28, 2006 at 05:39 UTC
    Coding style is about writing code for maintainability.Computers may be able to read that and discern that you attempted to dereference a scalar. But humans get head-aches looking at it.

    Compare what you offered, above, with:

    my ( $my_tgid_list, $my_switchClli_list, $my_btfn_list ); my ( $my_custClli_list, $my_trkCount_list, $ $my_ldn_list); my ( $my_location_list, $my_status_list, $my_flag_list); my ( $eachrow, $tmp_tgid, $tmp_switchClli, $tmp_btfn); my ( $tmp_custClli, $tmp_trkCount, $tmp_ldn); my ( $tmp_location, $tmp_flag );
    or

    my ( $my_tgid_list, $my_switchClli_list); my ( $my_btfn_list, $my_custClli_list ); my ( $my_trkCount_list, $ $my_ldn_list); my ( $my_location_list, $my_status_list); my ( $my_flag_list, $eachrow); my ( $tmp_tgid, $tmp_switchClli); my ( $tmp_btfn, $tmp_custClli); my ( $tmp_trkCount, $tmp_ldn); my ( $tmp_location, $tmp_flag );
    that code would give you an error identified by a line number which would not tax the attention span to decipher. (Speaking of which, will variable names like $tmp_btfn mean enough to you six months from now when you next look at this, that you won't have to decipher it?) Indeed, the very visual presentation of the code might have helped the human writing it to see the attempt to derefence $ $my_ldn_list, when coding the next line or three. I hadn't realized that the compiler would discard that extra space.

    There is a lot I am still learning about how this compiler thingy works. I'm not a real perl hacker, like those folks who keep refactoring perl5 to make it into perl6. I'm just the kind of perl hacker who reads perldocs and learns how to use the perl5 that Creation and Its Servants saw fit to make available to help us get our job done. I'm finally starting to understand that if I can read it the computer can too.

    -- Hugh