Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Script/module formatting gotcha

by mjscott2702 (Pilgrim)
on Oct 06, 2010 at 13:25 UTC ( #863787=perlquestion: print w/replies, xml ) Need Help??
mjscott2702 has asked for the wisdom of the Perl Monks concerning the following question:

Esteemed Monks, While reading through a module I have inherited, I was baffled why the following could compile, and not even generate any warnings:
my $var1 = # comment line1 # comment line2 # .... # comment line100 my $var2 = 0;
Then, of course, I realised that the compiler was simply skipping over the comments (and newlines) to interpret this as my $var1 = my $var2 = 0; which is perfectly legal syntax, of course.

I don't have access to utilities such as perltidy or the Perl::Critic module on this system - my question is - would they even have helped in this case?

Replies are listed 'Best First'.
Re: Script/module formatting gotcha
by jettero (Monsignor) on Oct 06, 2010 at 13:39 UTC
    I use this form quite often... my ($val) = () = something; I also use $this->{key} = my $val = 7;; it's not a bug. The assignment has a value of the rvalue. Neat!! If you understand this, I don't understand the question... What if I wanted to do this?
    $this->{key} = # this is an important note about the next assignment my $val = blarg();
    Should I have to then tell critic to ## no critic that?
      The problem is not that the construction as such is problematic, but that the format of the source-code makes it had to see immediately what is happening (at least that is how I understand the question).

      And I am afreid that neither perltidy nor perlcritic would have helped a lot here.

      But perl -MO=Deparse <your script> would have.

      It's not the construct of assigning an assignment to a variable that I am questioning here, I too use this all the time.

      The source of my confusion were the hundreds of comment lines between the left and right-hand sides. IMHO, this is a way of making the code more unreadable - yes comments can do that! In your example (which, to be fair has only a single comment line, and is less likely to be confusing), I think it would be clearer to write:

      $this->{key} = my $val = blarg(); # this is an important note about the previous +assignment
Re: Script/module formatting gotcha
by ww (Archbishop) on Oct 06, 2010 at 13:51 UTC
    further re jethro's...

    Alternately, is the question 'how do I reformat/relocate the comment lines so they're not in the middle of the assignment, like yo?

    # assign to $var1 the value of $var2: # comment line1 # comment line2 # .... # comment line100 my $var1 = my $var2 = 0;

    My suspicion is that that approach to dealing with such infelicitous formating would require a truly wise AI module ...
    or active if tedious utilization of the connection between brain and fingers.

      I have to protest profoundly to this blatant misuse of my most illustrious name in connection with riff-raff like jettero ;-)
      Agreed, and in this case the AI between the chair and keyboard was the source of the confusion. However, I think it may be a valid as a style to avoid, in terms of readability.
Re: Script/module formatting gotcha
by oko1 (Deacon) on Oct 06, 2010 at 14:06 UTC

    As jettero pointed out, it's an rvalue assignment - and although the code in the example that you cite isn't doing anything worthwhile, the mechanism itself is a perfectly valid and useful one. Without it, you couldn't do things like

    die "Syntax: [...]\n" unless my $fname = shift;

    and so on. I think that the right perspective here is that the tools are available and usable, but the decision about whether something is useful or not is up to the human. :)

    "Language shapes the way we think, and determines what we can think about."
    -- B. L. Whorf
      Understood, and I have no problem with the rvalue itself being an lvalue (if that makes sense).

      My only point here was that splitting this construct across multiple lines, with a large number of comments in-between, makes the script difficult to read - the construct itself is clear, and useful :)

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2018-04-19 10:30 GMT
Find Nodes?
    Voting Booth?