FAQ
Hi All,
Reading through Learning Perl (3rd Ed), and messing around with Binary
Assignment Operators (2.5.3).

Code is as follows :
#!perl -w
$fred = 4;
print "Fred is now : $fred \n";
$fred += 4;
print "Add 4 to Fred : $fred \n";
$fred *= 2;
print "Multiply by 2 : $fred \n";
$fred -= 6;
print "Subtract 6 : $fred \n";
$fred /= 2;
print "Divide by 2 : $fred \n";
#$fred .= 5
$fred = $fred . 5
print "$fred \n";

And I get the following error :

C:\SCRIPTS\test>perl fred.pl
syntax error at fred.pl line 14, near "print"
Execution of fred.pl aborted due to compilation errors.

What I'm trying to do is to append 5 to the value of $fred. If you
comment out the last 3 lines, you get 5 as the value for $fred.

Is this a valid assignment? Is it possible to paste in a number to the
end of another number to make a new number? (In this case, 55).

I've also tried encapsulating the value of 5 with and without '' "" and ``

And I don't reckon changing Fred into Barney will work either :o)

Thanks in advance,

DerekB


************************************************************************
Meteor web site http://www.meteor.ie
************************************************************************

Search Discussions

  • LoBue, Mark at Jun 26, 2003 at 11:45 pm

    -----Original Message-----
    From: Derek Byrne
    Sent: Thursday, June 26, 2003 4:15 PM
    To: 'beginners@perl.org'
    Subject: FW: Re : Compilation Errors


    Just a note - running win2k and perl v5.8.0 (built for
    MSWin32-x86-multi-thread).

    -----Original Message-----
    From: Derek Byrne
    Sent: 27 June 2003 00:13
    To: beginners@perl.org
    Subject: Re : Compilation Errors


    Hi All,
    Reading through Learning Perl (3rd Ed), and messing around
    with Binary
    Assignment Operators (2.5.3).

    Code is as follows :
    #!perl -w
    $fred = 4;
    print "Fred is now : $fred \n";
    $fred += 4;
    print "Add 4 to Fred : $fred \n";
    $fred *= 2;
    print "Multiply by 2 : $fred \n";
    $fred -= 6;
    print "Subtract 6 : $fred \n";
    $fred /= 2;
    print "Divide by 2 : $fred \n";
    #$fred .= 5
    $fred = $fred . 5
    At this point, you are "stringifying" $fred, so this should be:

    $fred = $fred . '5';

    -Mark
  • Derek Byrne at Jun 26, 2003 at 11:49 pm
    Oopsie - just saw my own mistake, forgot to add the ; at the end of the line
    preceding the last print.. Doh!
    Thank you Mark, just tried it again, and it works without the '' surrounding
    the 5.. to be proper, should it have the ''?

    -----Original Message-----
    From: LoBue, Mark
    Sent: 27 June 2003 00:45
    To: 'Derek Byrne'; 'beginners@perl.org'
    Subject: RE: Re : Compilation Errors

    -----Original Message-----
    From: Derek Byrne
    Sent: Thursday, June 26, 2003 4:15 PM
    To: 'beginners@perl.org'
    Subject: FW: Re : Compilation Errors


    Just a note - running win2k and perl v5.8.0 (built for
    MSWin32-x86-multi-thread).

    -----Original Message-----
    From: Derek Byrne
    Sent: 27 June 2003 00:13
    To: beginners@perl.org
    Subject: Re : Compilation Errors


    Hi All,
    Reading through Learning Perl (3rd Ed), and messing around
    with Binary
    Assignment Operators (2.5.3).

    Code is as follows :
    #!perl -w
    $fred = 4;
    print "Fred is now : $fred \n";
    $fred += 4;
    print "Add 4 to Fred : $fred \n";
    $fred *= 2;
    print "Multiply by 2 : $fred \n";
    $fred -= 6;
    print "Subtract 6 : $fred \n";
    $fred /= 2;
    print "Divide by 2 : $fred \n";
    #$fred .= 5
    $fred = $fred . 5
    At this point, you are "stringifying" $fred, so this should be:

    $fred = $fred . '5';

    -Mark


    ************************************************************************
    Meteor web site http://www.meteor.ie
    ************************************************************************
  • LoBue, Mark at Jun 26, 2003 at 11:50 pm

    -----Original Message-----
    From: Derek Byrne
    Sent: Thursday, June 26, 2003 4:44 PM
    To: 'LoBue, Mark'; Derek Byrne; 'beginners@perl.org'
    Subject: RE: Re : Compilation Errors


    Oopsie - just saw my own mistake, forgot to add the ; at the
    end of the line
    preceding the last print.. Doh!
    Thank you Mark, just tried it again, and it works without the
    '' surrounding
    the 5.. to be proper, should it have the ''?
    Perhaps perl is doing that for you also, it is just so smart, since the . is
    a string operator, it converts both arguments to strings.

    -Mark
  • Derek Byrne at Jun 27, 2003 at 12:04 am
    Last question on this, but, is there anything I should be aware of if I code
    the Fred prog like this :

    #!perl -w
    $Add = 4;
    $Mul = 2;
    $Sub = 6;
    $Div = 2;
    $Append = 5;
    $fred = 4;

    print "Fred is now : $fred \n";
    $fred += $Add;
    print "Add 4 to Fred : $fred \n";
    $fred *= $Mul;
    print "Multiply by 2 : $fred \n";
    $fred -= $Sub;
    print "Subtract 6 : $fred \n";
    $fred /= $Div;
    print "Divide by 2 : $fred \n";
    $fred .= $Append;
    print "Append 5 to Fred : $fred \n";

    -----Original Message-----
    From: LoBue, Mark
    Sent: 27 June 2003 00:51
    To: 'Derek Byrne'; 'beginners@perl.org'
    Subject: RE: Re : Compilation Errors

    -----Original Message-----
    From: Derek Byrne
    Sent: Thursday, June 26, 2003 4:44 PM
    To: 'LoBue, Mark'; Derek Byrne; 'beginners@perl.org'
    Subject: RE: Re : Compilation Errors


    Oopsie - just saw my own mistake, forgot to add the ; at the
    end of the line
    preceding the last print.. Doh!
    Thank you Mark, just tried it again, and it works without the
    '' surrounding
    the 5.. to be proper, should it have the ''?
    Perhaps perl is doing that for you also, it is just so smart, since the . is
    a string operator, it converts both arguments to strings.

    -Mark


    ************************************************************************
    Meteor web site http://www.meteor.ie
    ************************************************************************
  • R. Joseph Newton at Jun 28, 2003 at 6:10 pm

    Derek Byrne wrote:

    Last question on this, but, is there anything I should be aware of if I code
    the Fred prog like this : Yes
    #!perl -w
    use strict; # always
    use warnings; # unless you fully understand why you are turning them off.

    The strict and warnings commands will save you a lot of trouble. They may also
    make things harder at first, as you will have to code correctly or raise errors.
    my $add = 4;
    use my to indicate that each variable you declare belongs to the current scope,
    rather than being inherited from some outer scope. Always scope your variables
    as narrowly as possible. Also, the coding conventions in Perl call for
    lowercase variable names for lexically scoped variables.
    $mul = 2; # ditto
    ...
    Joseph

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedJun 26, '03 at 11:17p
activeJun 28, '03 at 6:10p
posts6
users3
websiteperl.org

People

Translate

site design / logo © 2022 Grokbase