Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

use constant usage clarification

by perlron (Pilgrim)
on Oct 31, 2014 at 06:10 UTC ( [id://1105698]=perlquestion: print w/replies, xml ) Need Help??

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

Hello Monks
I am not able to understand the following line of code.
use constant x => 1/3 in the context that the following line of code gives a scalar assignment error
use constant x = 1/3
From what i have read in perldoc -f use, the former is a pragma. I tried to check in the perlpragma for this usage reference, but i could only such case where the => was mentioned , without a clear reference as to its usage.
The background is im writing a file to a PDF, and while reading a document on how to use PDF:API2 and i came across this code instance which seemed relevant to fully understand

The temporal difficulty with perl is u need to know C well to know the awesome.else u just keep *using* it and writing inefficient code

Replies are listed 'Best First'.
Re: use constant usage clarification
by Loops (Curate) on Oct 31, 2014 at 06:19 UTC

    Hi perlron,

    from the perlop page:
    The => operator is a synonym for the comma except that it causes a word on its left to be interpreted as a string if it begins with a letter or underscore and is composed only of letters, digits and underscores. This includes operands that might otherwise be interpreted as operators, constants, single number v-strings or function calls. If in doubt about this behavior, the left operand can be quoted explicitly.

    So there is no assignment going on (at that point), just a list of values being passed to the pragma. It's the same as this:

    use constant 'x',  1/3;

    The pragma then does the magic of turning each identifier,value pair into a usable constant.


      Ok .thanks . nothing to worry for me about => then..

      The temporal difficulty with perl is u need to know C well to know the awesome.else u just keep *using* it and writing inefficient code
Re: use constant usage clarification (Deparse)
by Anonymous Monk on Oct 31, 2014 at 07:38 UTC

    I am not able to understand the following line of code.

    Its right there in Basic debugging checklist , B::Deparse

    $ perl -MO=Deparse,-p -e " use constant X => 1/3; " use constant ('X', 0.33333333333333331); -e syntax OK

    When you're not sure about perl syntax, get App::PPI::Dumper

    ppi_dumper / wxPPIxregexplain.pl

    $ cat 2 use constant X => 1/3; $ ppi_dumper 2 PPI::Document PPI::Statement::Include PPI::Token::Word 'use' PPI::Token::Whitespace ' ' PPI::Token::Word 'constant' PPI::Token::Whitespace ' ' PPI::Token::Word 'X' PPI::Token::Whitespace ' ' PPI::Token::Operator '=>' PPI::Token::Whitespace ' ' PPI::Token::Number '1' PPI::Token::Operator '/' PPI::Token::Number '3' PPI::Token::Structure ';' PPI::Token::Whitespace '\n' PPI::Token::Whitespace '\n'
    So what is  =>? Its an PPI::Token::Operator ... see perlop
Re: use constant usage clarification
by BrowserUk (Patriarch) on Oct 31, 2014 at 06:21 UTC
    [this]' gives a scalar assignment error use constant x = 1/3

    Really? Cos when I try it, the error is: Unquoted string "x" may clash with future reserved word.

    Care to post a short script that produces that error when run?


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Hello BrowserUK i'm receiving too an assignement error using the wrong syntax with the not stringified left value:
      #(something like:) perl -MConfig -e "BEGIN{print 'Perl ', map {$Config{$_},' '}qw/version + os archname/ }; use constant X = 1/3;print qq(\n),+X,qq(\n)" ----------------------------------------······· | Can't modify constant item in scalar assignment at -e line 1, near +"3;" | BEGIN not safe after errors--compilation aborted at -e line 1. | Perl 5.14.2 MSWin32-x86-multi-thread -------------------------------------------------------------------- +---······· ----------------------------------------······· | Can't modify constant item in scalar assignment at -e line 1, near +"3;" | BEGIN not safe after errors--compilation aborted at -e line 1. | Perl 5.20.0 MSWin32-x86-multi-thread-64int -------------------------------------------------------------------- +---······· ----------------------------------------······· | Can't modify constant item in scalar assignment at -e line 1, near +"3;" | BEGIN not safe after errors--compilation aborted at -e line 1. | Perl 5.16.2 MSWin32-x64-multi-thread -------------------------------------------------------------------- +---·······

      L*
      There are no rules, there are no thumbs..
      Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1105698]
Approved by Loops
Front-paged by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2024-04-16 19:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found