Translate BASH source

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
Will 'require' serve as a translation for Bash 'source' ('.').

I am trying translate a BASH script.

The Bash script includes something like

 . filefoo

where filefoo is a whole bunch of variable assignments.

It is easy enough to translate the variable assignments to perl.

If I tanslate filefoo and stick a 'return 1;' at the bottom, would 'require
filefoo' be a reasonable translation of the Bash . filefoo?

Lars Eighner <
     Give me chastity and continence, but not just now.  -- St. Augustine

Re: Translate BASH source

Quoted text here. Click to load it

'require' has slightly different semantics. In particular, 'require'
will always (and only) search the directories listed in @INC, whereas
'.' will search $PATH iff filefoo doesn't contain a slash.

If you need to keep the 'search $PATH' semantics you'll need to
implement it yourself. If you just want to load a file from the given
path (without doing any searching), use 'do' instead. If you're happy to
install filefoo somewhere in @INC instead of somewhere in $PATH, use

Quoted text here. Click to load it

Well... it's easy enough to translate them into *bad* Perl. If you
follow the advice everyone in this group gives you and 'use strict' and
declare your variables with 'my', you'll find you can't get at them from
outside the file they're declared in. This is a feature :).

A simple way round, if you're just looking to get the script working
rather than write it well, is to declare the variables with 'our' in
both files. A better answer would be to declare a single %config hash,
and have your included file set that rather than a whole lot of separate
variables. A much better answer would be to use some well-known config
file format, and something like Config::Any to load it for you.

Quoted text here. Click to load it

It's not usual to use 'return' when ending a required file with a true
value; a line just containing '1;' at the end of the file is much more
common. While you can use 'return' to leave a required file partway
through, I'd consider that very confusing behaviour.


   If you put all the prophets,   |   You'd have so much more reason
   Mystics and saints             |   Than ever was born
   In one room together,          |   Out of all of the conflicts of time.                                    The Levellers, 'Believers'

Site Timeline