| 1 | package Panotools::Makefile::Variable; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | Panotools::Makefile::Variable - Assemble Makefile Variable definitions | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | Simple interface for generating Makefile syntax | ||
| 10 | |||
| 11 | =head1 DESCRIPTION | ||
| 12 | |||
| 13 | Writing Makefiles directly from perl scripts with print and "\t" etc... is | ||
| 14 | prone to error, this library provides a simple perl interface for assembling | ||
| 15 | Makefiles. | ||
| 16 | |||
| 17 | =cut | ||
| 18 | |||
| 19 | use strict; | ||
| 20 | use warnings; | ||
| 21 | |||
| 22 | use Panotools::Makefile::Utils qw/quotetarget quoteprerequisite quoteshell/; | ||
| 23 | |||
| 24 | =head1 USAGE | ||
| 25 | |||
| 26 | $var = new Panotools::Makefile::Variable; | ||
| 27 | |||
| 28 | ..or define the 'variable name' at the same time: | ||
| 29 | |||
| 30 | $var = new Panotools::Makefile::Variable ('USERS'); | ||
| 31 | |||
| 32 | ..or define the name and values at the same time: | ||
| 33 | |||
| 34 | $var = new Panotools::Makefile::Variable ('USERS', 'Andy Pandy'); | ||
| 35 | |||
| 36 | =cut | ||
| 37 | |||
| 38 | sub new | ||
| 39 | { | ||
| 40 | my $class = shift; | ||
| 41 | $class = ref $class || $class; | ||
| 42 | my $self = bless {name => shift, value => [@_]}, $class; | ||
| 43 | return $self; | ||
| 44 | } | ||
| 45 | |||
| 46 | =pod | ||
| 47 | |||
| 48 | Set or query the name: | ||
| 49 | |||
| 50 | $var->Name ('USERS'); | ||
| 51 | $text = $var->Name; | ||
| 52 | |||
| 53 | =cut | ||
| 54 | |||
| 55 | sub Name | ||
| 56 | { | ||
| 57 | my $self = shift; | ||
| 58 | $self->{name} = shift if @_; | ||
| 59 | return $self->{name}; | ||
| 60 | } | ||
| 61 | |||
| 62 | sub NameRef | ||
| 63 | { | ||
| 64 | my $self = shift; | ||
| 65 | return '$('. $self->Name .')'; | ||
| 66 | } | ||
| 67 | |||
| 68 | sub NameRefShell | ||
| 69 | { | ||
| 70 | my $self = shift; | ||
| 71 | return '$('. $self->Name .'_SHELL)'; | ||
| 72 | } | ||
| 73 | |||
| 74 | =pod | ||
| 75 | |||
| 76 | $var->Values ('James Brine', 'George Loveless'); | ||
| 77 | $var->Values ('Thomas Standfield'); | ||
| 78 | |||
| 79 | =cut | ||
| 80 | |||
| 81 | sub Values | ||
| 82 | { | ||
| 83 | my $self = shift; | ||
| 84 | push @{$self->{value}}, @_; | ||
| 85 | return $self->{value}; | ||
| 86 | } | ||
| 87 | |||
| 88 | =pod | ||
| 89 | |||
| 90 | Construct a text fragment that defines this variable suitable for use in a | ||
| 91 | Makefile like so: | ||
| 92 | |||
| 93 | $text = $var->Assemble; | ||
| 94 | |||
| 95 | =cut | ||
| 96 | |||
| 97 | sub Assemble | ||
| 98 | { | ||
| 99 | my $self = shift; | ||
| 100 | return '' unless defined $self->{name}; | ||
| 101 | |||
| 102 | my $text; | ||
| 103 | $text .= quotetarget ($self->{name}); | ||
| 104 | $text .= ' = '; | ||
| 105 | $text .= join ' ', (map { quotetarget ($_)} grep /./, @{$self->{value}}); | ||
| 106 | $text .= "\n"; | ||
| 107 | $text .= quotetarget ($self->{name} .'_SHELL'); | ||
| 108 | $text .= ' = '; | ||
| 109 | my @items = map { quoteshell ($_)} grep /./, @{$self->{value}}; | ||
| 110 | @items = map {s/\$\(([^)]+)\)/\$($1_SHELL)/g; $_} @items; | ||
| 111 | $text .= join ' ', @items; | ||
| 112 | $text .= "\n"; | ||
| 113 | return $text; | ||
| 114 | } | ||
| 115 | |||
| 116 | 1; |