File: mason.pl

package info (click to toggle)
libhtml-mason-perl 1%3A1.60-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,816 kB
  • sloc: perl: 8,621; sh: 49; makefile: 2
file content (75 lines) | stat: -rwxr-xr-x 2,063 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/perl

use strict;
use HTML::Mason '1.11';
use File::Basename qw(dirname basename);
use File::Spec ();
use Cwd        ();

my ( $params, $component, $args ) = parse_command_line(@ARGV);

# Set a default comp_root
unless ( exists $params->{comp_root} ) {
    if ( File::Spec->file_name_is_absolute($component) ) {
        $params->{comp_root} = dirname($component);
        $component = '/' . basename($component);
    }
    else {
        $params->{comp_root} = Cwd::cwd;

        # Convert local path syntax to slashes
        my ( $dirs, $file ) = ( File::Spec->splitpath($component) )[ 1, 2 ];
        $component = '/' . join '/', File::Spec->splitdir($dirs), $file;
    }
}

my $interp = HTML::Mason::Interp->new(%$params);
$interp->exec( $component, @$args );

#######################################################################################
sub parse_command_line {
    die usage() unless @_;

    my %params;
    while (@_) {
        if ( $_[0] eq '--config_file' ) {
            shift;
            my $file = shift;
            eval { require YAML; 1 }
                or die
                "--config_file requires the YAML Perl module to be installed.\n";
            my $href = YAML::LoadFile($file);
            @params{ keys %$href } = values %$href;

        }
        elsif ( $_[0] =~ /^--/ ) {
            my ( $k, $v ) = ( shift, shift );
            $k =~ s/^--//;
            $params{$k} = $v;

        }
        else {
            my $comp = shift;
            return ( \%params, $comp, \@_ );
        }
    }

    die usage();
}

sub usage {
    return <<EOF;

 Usage: $0 [--param1 value1 ...] [--config_file file] component [arg1 arg2 ...]
  e.g.: $0 --comp_root /mason/comps component.mas
    or: $0 --config_file /mason/config.yaml component.mas foo 5 bar 3
 
    Use --config_file to specify any additional parameters.
 
    'comp_root' defaults to current directory if component has a 
    relative path, or to dirname(component) otherwise.
 
    See `perldoc HTML::Mason::Params` for a list of valid parameters.

EOF
}