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

try/finally syntax

by Superfox il Volpone (Sexton)
on Jun 23, 2024 at 12:58 UTC ( [id://11160181]=perlquestion: print w/replies, xml ) Need Help??

Superfox il Volpone has asked for the wisdom of the Perl Monks concerning the following question:

I'm on perl v5.34.1, with the "try" feature enabled. I still cannot figure out the correct syntax for:
my $current_directory = getcwd(); try { chdir($working_directory); } finally { chdir($current_directory); };
This fails with the error:
syntax error at /script.pl line 56, near "} finally " Execution of /script.pl aborted due to compilation errors.
```

Replies are listed 'Best First'.
Re: try/finally syntax
by ikegami (Patriarch) on Jun 23, 2024 at 15:21 UTC

    You have two errors that both result in that message:

    • the catch clause is missing, and
    • finally was only introduced in 5.36.

    The syntax is

    try BLOCK catch (VAR) BLOCK # 5.34+ try BLOCK catch (VAR) BLOCK finally BLOCK # 5.36+

    And note that as a flow control statement, it isn't terminated by a semi-colon.

Re: try/finally syntax
by Corion (Patriarch) on Jun 23, 2024 at 13:35 UTC

    try/finally came only with Perl 5.36. Perl 5.34 only knows try/catch.

      That's only half of it.

      $ perl -v | grep 'This is' This is perl 5, version 40, subversion 0 (v5.40.0) built for x86_64-li +nux-thread-multi $ perl -e'use feature qw( try ); try { } finally { }' try/catch/finally is experimental at -e line 1. syntax error at -e line 1, near "} finally" Execution of -e aborted due to compilation errors.
Re: try/finally syntax
by eyepopslikeamosquito (Archbishop) on Jun 23, 2024 at 13:46 UTC
Re: try/finally syntax
by eyepopslikeamosquito (Archbishop) on Jun 24, 2024 at 05:01 UTC

    > I'm on perl v5.34.1, with the "try" feature enabled

    From this 2015 PM node, I see (like me) you have a long-standing keen interest in Perl's try/catch exception handling. :-) That being the case, and assuming you're stuck using the system perl v5.34.1, why not simply leave that version of perl alone and build your own from the latest stable source release, namely perl v5.40.0?

    In my experience, this is an easy and reliable chore to perform on Linux, as described in detail at:

    👁️🍾👍🦟

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (None)
    As of 2024-09-10 05:34 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found

      Notices?
      erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.