Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Re: A C-like brain in a Perl-like world

by cyberscribe (Acolyte)
on Sep 28, 2001 at 10:59 UTC ( [id://115327]=note: print w/replies, xml ) Need Help??


in reply to Re: A C-like brain in a Perl-like world
in thread A C-like brain in a Perl-like world

Please explain "strict" to me. Perl seems anything but strict. Except when it comes to if statements, which MUST involve curly braces:

if(<cond>){%eval_true%}else{%eval_false%}

unlike shell script or other, looser constructs that know the next line off an if is %eval_true%.

I must believe there is a good reason for this that eludes me, as I must believe that Perl is about simplicity and elegance. Yet removing curly braces and getting away with it seems like a good, intuitive thing to do to a compiler/interpreter.

Pontificate?




p.s. I would like to propose a new tag for posts and cb ... 'pseudocode' - for formatting code that is really shorthand and differentiating it from stuff between legitimate 'code' tags, which should execute in a Perl environment without errors, or with only errors specifically described in the post. What I wrote above was 'pseudocode.

Replies are listed 'Best First'.
Re: Re: Re: A C-like brain in a Perl-like world
by blakem (Monsignor) on Sep 28, 2001 at 11:49 UTC
    In perl you can use if as a statement modifier... Therefore, this is perfectly valid code:
    #!/usr/bin/perl -wT use strict; for my $var (0..9) { print "$var is even\n" if $var%2 == 0; # using if as a statement mo +difier }
    For more on use strict, please see strict.pm.

    -Blake

Re^3: A C-like brain in a Perl-like world
by Aristotle (Chancellor) on Sep 28, 2001 at 14:05 UTC

    Forcing a BLOCK rather than an instruction after an if is because of a simple reason: the "dangling else". It is not apparent in which way if $condition if $more_condition do_something() else do_other() is to be disambiguated; many tradiditional languages arbitrarily choose to bind the else branch to the closest if, but that's not readily apparent from looking at the code - and if you want to bind the else of the first if, you still have to use a BLOCK around the second.

    So Perl forces you to use BLOCKs for clarity, but lets you append an if clause as a statement modifier for when you want brevity: do_something if $condition;.

    <CODE> has more functions than formatting. For pseudocode, you can use the regular <tt></tt> HTML tags.

Log In?
Username:
Password:

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

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

    No recent polls found