Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Query about barewords

by kevinp (Novice)
on Sep 06, 2012 at 14:16 UTC ( [id://992099]=perlquestion: print w/replies, xml ) Need Help??

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

While putting together some examples of using Gtk and Perl I came across the following behaviour which I hope someone can explain.

This piece of code complains of a BAREWORD under strict.

$switch->signal_connect( notify::active => \&toggle );

As you would expect, if I add some quotes the code works with no complaints.

$switch->signal_connect( 'notify::active' => \&toggle );

So - my question is - why does the following work and not complain about barewords.

$update_button->signal_connect ( clicked => \&update );

It seems that Perl sees notify::active and clicked differently and that seems inconsistent to my mind.

Can someone explain the behaviour?

Replies are listed 'Best First'.
Re: Query about barewords
by Athanasius (Archbishop) on Sep 06, 2012 at 14:25 UTC

    From Comma Operator:

    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.

    Hope that helps,

    Athanasius <°(((><contra mundum

      Empirically, the documentation seems to be a little inaccurate as the fat comma also seems to work if the word on the left starts with a number and consists only of numbers.

      $ perl -Mstrict -Mwarnings -MData::Dumper -E ' > my %hash = ( > 1 => q{one}, > 11 => q{eleven} > ); > print Data::Dumper->Dumpxs( [ \ %hash ], [ qw{ *hash } ] );' %hash = ( '11' => 'eleven', '1' => 'one' ); $ $ perl -Mstrict -Mwarnings -MData::Dumper -E ' > my %hash = ( > 1 => q{one}, > 11 => q{eleven}, > 2t => q{fruity}, > ); > print Data::Dumper->Dumpxs( [ \ %hash ], [ qw{ *hash } ] );' syntax error at -e line 5, near "2t" Execution of -e aborted due to compilation errors. $ $ perl -Mstrict -Mwarnings -MData::Dumper -E ' > my %hash = ( > 1 => q{one}, > 11 => q{eleven}, > _2t => q{fruity}, > ); > print Data::Dumper->Dumpxs( [ \ %hash ], [ qw{ *hash } ] );' %hash = ( '11' => 'eleven', '1' => 'one', '_2t' => 'fruity' ); $

      I hope this is of interest.

      Cheers,

      JohnGG

        ...if the word on the left starts with a number and consists only of numbers.
        As long as you don't want to keep any leading zeros.
      The "bareword" may also start with a "-".

        It seems to treat as an octal number, converts to decimal and then stringifies by the look of it.

        $ perl -Mstrict -Mwarnings -MData::Dumper -E ' > my %hash = ( > 011 => q{nine}, > 11 => q{eleven}, > _2t => q{fruity}, > ); > print Data::Dumper->Dumpxs( [ \ %hash ], [ qw{ *hash } ] );' %hash = ( '11' => 'eleven', '_2t' => 'fruity', '9' => 'nine' ); $ $ perl -Mstrict -Mwarnings -MData::Dumper -E ' > my %hash = ( > 08 => q{eight}, > 11 => q{eleven}, > _2t => q{fruity}, > ); > print Data::Dumper->Dumpxs( [ \ %hash ], [ qw{ *hash } ] );' Illegal octal digit '8' at -e line 3, at end of line Execution of -e aborted due to compilation errors. $

        A zero on its own is fine.

        Update: I'm an idiot, I meant to post as a reply to this :-(

        Cheers,

        JohnGG

Re: Query about barewords
by remiah (Hermit) on Sep 06, 2012 at 16:52 UTC
    Hello.

    "clicked" may be a constant. It maybe imported when you declare to use some module.

    #!/usr/bin/perl use warnings; use strict; package notify; use constant { active => 2, }; 1; package main; use constant { clicked=>1, }; print clicked . "\n"; print notify::active . "\n"; exit;
    So, currently, you don't have package "notify" declaring "active" is a constant. If I am looking different direction, forgive me please.

Log In?
Username:
Password:

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

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

    No recent polls found